﻿using ProMvc.Domain;
using ProMvc.Models;
using ProMvc.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Newtonsoft.Json;
using PetaPoco;

namespace ProMvc.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/

        public ActionResult Index()
        {
            ViewBag.Greeting = "Hello world!!";
            //UserRepository rep = new UserRepository();
            //rep.Add();
            //rep.OrmAdd();
            //JsonConvert.SerializeObject()
            User u = new User() { UName = "ggg" };
            string json = JsonConvert.SerializeObject(u);
            ViewBag.JsonData = json;
            return View(u);
        }

        public string GetContent()
        {
            return "The first mvc project!";
        }

        public ViewResult GetViewResult()
        {
            return View();
        }

        [HttpGet]
        public ActionResult PartialViewPage(int id=1)
        {
            return View();
        }

        [HttpGet]
        public ActionResult RsvpForm()
        {
            return View();
        }

        [HttpPost]
        public ActionResult RsvpForm(GuestResponse guestResponse)
        {
            if(ModelState.IsValid)
            {
                return View("Thanks", guestResponse);
            }
            else
            {
                return View();
            }
            
        }

        public ActionResult FormData()
        {
            return View();
        }

        [HttpPost]
        public ActionResult AjaxPost(User user)
        {
            return View();
        }

        public ActionResult Paging()
        {
            return View();
        }

        [HttpPost]
        public ActionResult PagingResult(int pageNumber=1)
        {
            Database db = new Database("test");
            db.EnableAutoSelect = true;
            var sql = Sql.Builder.Where("1=1");
            var page = db.Page<User>(pageNumber, 2, sql);
            PagerInfo pager = new PagerInfo();
            pager.RecordCount = (int)page.TotalItems;
            pager.PageSize = (int)page.ItemsPerPage;
            pager.CurrentPageIndex = pageNumber;
            PagerQuery<PagerInfo, IEnumerable<User>> query = new PagerQuery<PagerInfo, IEnumerable<User>>(pager, page.Items);
            return View(query);
            
        }
    }
}
